using iTextSharp.text;
using iTextSharp.text.exceptions;
using iTextSharp.text.pdf;
using System;
using System.IO;

namespace PrintDemo
{
    class PdfUtils
    {
        const string ITextExamplesFolder = "iTextExamples";
        const string ResourcesFolder = "resources";

        public static string Author => "haogm";

        public static string GetBaseDir()        {
          
            return Environment.CurrentDirectory; 
        }    
        /// <summary>
        /// 创建列 插入文本内容
        /// </summary>
        /// <param name="table"></param>
        /// <param name="content"></param>
        /// <param name="font"></param>
        /// <param name="minimumHeight"></param>
        /// <param name="colspan"></param>
        /// <param name="rowspan"></param>
        public static void CreateCell(PdfPTable table, string content, Font font, int minimumHeight = 20, int colspan = 0, int rowspan = 0)
        {
            var cell = new PdfPCell(new Phrase(content, font));
            cell.UseAscender = true;// 设置可以居中
            cell.MinimumHeight = minimumHeight;// 设置单元格高度
            cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;// 设置水平居中
            cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;// 设置垂直居中
            if (rowspan != 0)
            {
                cell.Rowspan = rowspan; //行合并
            }
            if (colspan != 0)
            {
                cell.Colspan = colspan; //列合并
            }
            table.AddCell(cell);
        }
        /// <summary>
        /// 创建列 插入图片
        /// </summary>
        /// <param name="table"></param>
        /// <param name="image"></param>
        /// <param name="minimumHeight"></param>
        /// <param name="colspan"></param>
        /// <param name="rowspan"></param>
        public static void CreateCell(PdfPTable table, Image image, int minimumHeight = 3, int colspan = 0, int rowspan = 0)
        {           
            var cell = new PdfPCell(image,true);// 是否填充
            cell.Padding = 5.5f; // 设置二维码在单元格中的边距
            cell.UseAscender = true;// 设置可以居中
            cell.MinimumHeight = minimumHeight;// 设置单元格高度
            cell.HorizontalAlignment = Element.ALIGN_CENTER;// 设置水平居中
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;// 设置垂直居中
            if (rowspan != 0)
            {
                cell.Rowspan = rowspan; //行合并
            }
            if (colspan != 0)
            {
                cell.Colspan = colspan; //列合并
            }
            table.AddCell(cell);
        }    

        //生成二维码的路径
        public static string GetQRFileName()
        {
            return Path.Combine(GetOutputFolder(), $"Qr.jpg");
        }   

        public static string GetOutputFolder()
        {
            var dir = Path.Combine(GetBaseDir(), "bin", "out");
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            return dir;
        }

    
        public static string GetOutputFolderPdf()
        {
            var dir = Path.Combine(GetBaseDir(), "bin", "outpdf\\");
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            return dir;
        }


        /// <summary>
        /// 验证pdf文件
        /// </summary>
        /// <param name="file"></param>
        public static void VerifyPdfFileIsReadable(byte[] file)
        {
            PdfReader reader = null;
            try
            {
                reader = new PdfReader(file);
                var author = reader.Info["Author"] as string;
                if (string.IsNullOrWhiteSpace(author) || !author.Equals(Author))
                {
                    throw new InvalidPdfException("This is not a valid PDF file.");
                }
            }
            finally
            {
                reader?.Close();
            }
        }

        public static void VerifyPdfFileIsReadable(string filePath)
        {
            VerifyPdfFileIsReadable(File.ReadAllBytes(filePath));
        }

    }
}
